package com.jjb.saas.demo.enums.role;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.jjb.saas.framework.enums.annotation.EnumInfoAnnotation;
import com.jjb.saas.framework.enums.enums.BaseEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Fei.Yang
 * @date 2022/4/24
 */
@AllArgsConstructor
@EnumInfoAnnotation(chineseName = "角色所属编码", dictType = "sys_role_belong_enum")
public enum RoleBelongEnum implements BaseEnum {
    TENANT("TENANT", "租户"),
    OPERATION("OPERATION", "运营"),
    CAMPUS("CAMPUS", "校区"),
    UNIT("UNIT", "机构"),
    CHANNEL("CHANNEL", "渠道");
    @Getter
    @JsonValue
    @EnumValue
    private final String value;
    @Getter
    private final String label;



    @JsonCreator
    public static RoleBelongEnum fromValue(Object flag) {

        if (null == flag) {
            return null;
        }
        String key = null;
        if (flag instanceof Map) {
            key = ((Map<String, String>) flag).get("code");
        } else if (flag instanceof String) {
            key = (String) flag;
        }
        String finalKey = key;
        return Arrays.stream(RoleBelongEnum.values())
                .filter(anEnum -> anEnum.getValue().equals(finalKey))
                .findFirst().orElse(null);
    }

    private static final Map<String, RoleBelongEnum> VALUES = new HashMap<>();

    static {
        for (final RoleBelongEnum e : RoleBelongEnum.values()) {
            RoleBelongEnum.VALUES.put(e.getValue(), e);
        }
    }


    public RoleBelongEnum of(String code) {
        return VALUES.get(code);
    }
}
